import Vue from 'vue'
import Router from 'vue-router'
import Login from '../views/Login.vue'
import Main from '../views/Main.vue'
import Welcome from '../views/Welcome.vue'
import Users from '../views/Users.vue'
import Rights from '../views/Rights.vue'
import Roles from '../views/Roles.vue'
import Categories from '../views/Categories.vue'
import Goods from '../views/Goods.vue'
import Params from '../views/Params.vue'
import AddGoods from '../views/AddGoods.vue'
import Orders from '../views/Orders.vue'
import Reports from '../views/Reports.vue'

Vue.use(Router)

let router=new Router({
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      component : Login
    },
    {
      path: '/main',
      component : Main,
      children:[
        {path:'/welcome',component:Welcome},
        {path:'/users',component:Users},
        {path:'/roles',component:Roles},
        {path:'/rights',component:Rights},
        {path:'/categories',component:Categories},
        {path:'/params',component:Params},
        {path:'/goods',component:Goods},
        {path:'/goods/add',component:AddGoods},
        {path:'/orders',component:Orders},
        {path:'/reports',component:Reports},
      ],
      redirect:'/welcome'
    }
  ]
})

// 添加全局路由守卫
router.beforeEach((to,from,next)=>{
  // to代表你要去路径
  // from代表当前路径
  // next() 放行    next('/login')去指定路径

  if(to.path==='/login'){
    next()
  }else{
    // 拿一下token
    let curToken=window.sessionStorage.getItem('token');
    // 判断token是否存在，如果存在token，证明你登录了！
    if(!curToken){
      next('/login')
    }else{
      next()
    }
  }
  
})

export default router
